<style scoped src="./index.scss" lang="scss" rel="stylesheet/scss"></style>
<template src="./index.html"></template>
<script>
export default {
  mixins: [Vue.prototype.$vFN.Mixins.Base, Vue.prototype.$vFN.Mixins.Edit],
  components: {
    SelectDataGoods: async () =>
      Vue.extend(await Vue.prototype.$vFN.Components.Base.SelectDataGoods()),
    SelectDataGoodsGroup: async () =>
      Vue.extend(
        await Vue.prototype.$vFN.Components.Base.SelectDataGoodsGroup()
      ),
    SelectDataGoodsClassify: async () =>
      Vue.extend(
        await Vue.prototype.$vFN.Components.Base.SelectDataGoodsClassify()
      ),
  },
  data() {
    return {
      appWindow: window,
      form: {
        // 优惠类型
        couponType: 1,
        // 优惠券标题
        couponName: "",
        // 优惠券描述
        couponDesc: "",
        // 使用时间(1时间范围 2有效天数(切换))
        expireType: 1,
        // 开始时间
        startTimeStr: "",
        endTimeStr: "",

        // 适用类目
        couponProductCategoryLst: [],
        // 适用分组
        couponProductGroupsLst: [],
        // 适用商品
        couponProductLst: [],
        // 应用类型 1全部商品 2商品分组 3商品分类 4指定商品
        applyType: 1,

        // 每人限领
        limitCount: 1,
        // 减免金额
        discountAmount: "",
        // 使用门槛
        useOrderAmount: "",
        // 库存
        stock: "",
        // 领取说明
        useNote: "",
        // 优惠说明
        discountNote: "",
        // 使用须知
        useRuleNote: "",
        validDays: "",
        deductOverlayLst: [], //下单可用抵扣id数组

        isOpen: 1,
      },

      isDisabled: false,

      // 页面接口地址
      apiUrl: {
        create: "api/coupon/createCoupon",
        update: "api/coupon/updateCoupon",
        detail: "api/coupon/getCoupon",
      },

      deducteList: [], //抵扣的资产类型的数组
    };
  },
  methods: {
    // 初始化
    async init() {
      await this.getIntegralType();
      // 初始化数据
      this.mixinEdit_editBoxInit({
        model: "form",
        id: this.id,
      });
    },

    // form 提交添加/修改 的参数扩展方法
    mixinEdit_editFormSubmitExtend(data_) {
      if (data_.applyType == 2) {
        data_.couponProductGroupsLst = [
          {
            groupsId: data_.couponProductGroupsLst[0],
            subGroupsId: data_.couponProductGroupsLst[1],
          },
        ];

        // 分类请空
        data_.couponProductCategoryLst = [];
      } else if (data_.applyType == 3) {
        data_.couponProductCategoryLst = [
          {
            categoryId: data_.couponProductCategoryLst[0],
            subCategoryId: data_.couponProductCategoryLst[1],
          },
        ];

        // 分组请空
        data_.couponProductGroupsLst = [];
      } else if (data_.applyType == 4) {
        let _list = [];
        data_.couponProductLst.forEach((item) => {
          _list.push({
            productId: item,
          });
        });
        data_.couponProductLst = _list;

        // 分组和分类请空
        data_.couponProductCategoryLst = [];
        data_.couponProductGroupsLst = [];
      }

      // 根据使用时间类型清空参数
      if (data_.expireType === 2) {
        data_.startTimeStr = "";
        data_.endTimeStr = "";
      } else if (data_.expireType === 1) {
        data_.validDays = "";
      }

      // 如果没选择全部商品 并且 没选择对应数据， 不提交
      if (
        data_.applyType !== 1 &&
        data_.couponProductLst.length == 0 &&
        data_.couponProductGroupsLst.length == 0 &&
        data_.couponProductCategoryLst.length == 0
      ) {
        this.$vFN.Tools.messageError("请选择适用的商品");
        return;
      }
      console.error("2", data_);

      // 如果是兑换券  折扣值为空
      if (data_.couponType == 3) {
        data_.discountAmount = "";
      }
      return data_;
    },

    // 编辑修改的参数扩展方法
    mixinEdit_editDataExtend(data_) {
      // console.log(1111,data_)

      // 添加
      if (this.mixinEdit_editType == "add") {
        this.isDisabled = false;
      }
      // 修改
      else if (this.mixinEdit_editType == "update") {
        this.isDisabled = true;
        if(data_.couponDesc == null) {
          data_.couponDesc = "";
        }
        if(data_.useNote == null) {
          data_.useNote = "";
        }
      }
      // 复制
      else if (this.mixinEdit_editType == "copy") {
        this.isDisabled = false;
      }

      if (
        this.mixinEdit_editType == "update" ||
        this.mixinEdit_editType == "copy"
      ) {
        // 处理返回的抵扣资产id的数组
        this.disposeIntegralType(data_);
        // 处理返回的优惠券对应商品内容
        if (!this.$vFN.Utils.isEmpty(data_.couponProductGroupsLst)) {
          let newArr = [];
          newArr.push(data_.couponProductGroupsLst[0].groupsId);

          if (data_.couponProductGroupsLst[0].subGroupsId) {
            newArr.push(data_.couponProductGroupsLst[0].subGroupsId);
          }

          data_.couponProductGroupsLst = newArr;
        }
        if (!this.$vFN.Utils.isEmpty(data_.couponProductCategoryLst)) {
          let newArr = [];
          newArr.push(data_.couponProductCategoryLst[0].categoryId);

          if (data_.couponProductCategoryLst[0].subCategoryId) {
            newArr.push(data_.couponProductCategoryLst[0].subCategoryId);
          }

          data_.couponProductCategoryLst = newArr;
        }
        if (!this.$vFN.Utils.isEmpty(data_.couponProductLst)) {
          data_.couponProductLst = data_.couponProductLst.map((item) => {
            return item.productId;
          });
        }
      }

      return data_;
    },

    async getIntegralType() {
      this.mixinEdit_loading = true;
      let res = await this.$vFN.Http.post(
        {
          url: "api/coupon/getIntegralType",
        },
        window
      );
      this.mixinEdit_loading = false;
      if (res._ok) {
        this.deducteList = res.data.map((item) => {
          return {
            ...item,
            isChecked: false,
          };
        });
        console.log("deducteList", this.deducteList);
      }
    },

    // 处理返回的资产id数组  判断是否选中
    disposeIntegralType(data_) {
      this.form.deductOverlayLst = data_.deductOverlayLst;

      console.error(this.deducteList);

      this.deducteList.forEach((item) => {
        item.id = String(item.id);

        let findIndex = this.form.deductOverlayLst.indexOf(item.id);

        // 大于-1表示id数组里面有此项
        if (findIndex > -1) {
          item.isChecked = true;
        }
      });

      // console.log("this.deducteList", this.deducteList);
    },

    radioChange(label_) {
      console.log("label_", label_);
      if (label_ == 2) {
        this.form.discountAmount = 10;
      }
    },
  },
  mounted() {
    this.init();
  },
  activated() {},
};
</script>